<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$feildata=array('id','uid','snmid','fromuser','app_uid','username','mobile','realmobile','nickname','avatar','onemid');
if($_RQ['op']=='list'){
	$return['ischecklogin']=model('core/core')->checklogin('','',true);
	$return['forward_url']=$_SESSION['forward_url'];
	$return['member']=$_SESSION['member'];
	$return['openid']=$_SESSION['openid'];
	$_SESSION['app_uid']=$_SESSION['app_uid']?:$_SESSION['uid'];
	$params=array('uniacid'=>$_SESSION['uniacid'],'app_uid'=>$_SESSION['app_uid'],'mid <>'=>$_SESSION['uid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('core_members_my',$params,array($pindex,$psize),$total,'','','id desc');
	if($list){
		foreach($list as &$item){
			$myid=$item['id'];
			$item=pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),$feildata);
			$item['avatar']=tomedia($item['avatar']);
			$item['myid']=$myid;
		}
	}
	if($_SESSION['app_uid']==$_SESSION['uid']){
		$isadd=pdo_get('core_members_my',array('app_uid'=>$_SESSION['app_uid'],'mid'=>$_SESSION['app_uid'],'uniacid'=>$_SESSION['uniacid']));
		if(!$isadd['id']){
			$data=array(
				'app_uid'=>$_SESSION['app_uid'],
				'mid'=>$_SESSION['app_uid'],
				'createtime'=>time(),
			);
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('core_members_my',$data);
		}
	}
	$return['list']=$list;
	set_html('title','我的账号');
	exi($return);
}
if($_RQ['op']=='edit'){
	$setting=uni_setting('register');
	$setting['login_type']=intval($setting['login_type']);
	$return['name_title']=$setting['name_mobile']?'手机号':'用户名';
	$return['item']=array();
	set_html('title','添加账号');
	exi($return);
}
if($_RQ['op']=='post'){
	$setting=uni_setting('register');
	$return['name_title']=$setting['name_mobile']?'手机号':'用户名';
	$post=get_postdata();
	//账号密码登陆
	if(!$post['username']){
		exi('请输入'.$return['name_title'].'！','error');
	}
	if(!$post['password']){
		exi('请输入密码！','error');
	}
	$feildata[]='salt';
	$feildata[]='password';
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'username'=>$post['username']),$feildata);
	if(empty($member)){
			if(preg_match("/^1[3456789]{1}\d{9}$/",$post['username'])){
				$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'mobile'=>$post['username']),$feildata);
				if(empty($member)){
					exi('手机号不存在，请先注册!','error');
				}
			}else{
				exi('用户不存在，请先注册!','error');
			}
	}
	if($_SESSION['uid']==$member['id']){
		exi('不能添加自己!','error');	
	}
	$isadd=pdo_get('core_members_my',array('app_uid'=>$_SESSION['app_uid'],'mid'=>$member['id'],'uniacid'=>$_SESSION['uniacid']));
	if($isadd['id']){
		exi('该账号已添加，请勿重复!','error');
	}
	if(!check_password($post['password'],$member['salt'],$member['password'])){
		exi($return['name_title'].'或密码错误!','error');
	}
	$data=array(
		'app_uid'=>$_SESSION['app_uid'],
		'mid'=>$member['id'],
		'createtime'=>time(),
	);
	$data['uniacid']=$_SESSION['uniacid'];
	pdo_insert('core_members_my',$data);
	exi('设置成功！','','core/member.mymid/');
}
if($_RQ['op']=='login'){
	$post=get_postdata();
	model('core/member')->login($post['id']);
	if($post['forward']){
		$forward_url=custom_url($post['forward'],true);
	}elseif($_SESSION['forward_url']){
		$forward_url=$_SESSION['forward_url'];
	}else{
		$forward_url='core/member.mymid/';
	}
	exi('切换成功！','',$forward_url);
}
if($_RQ['op']=='delete'){
	$post=get_postdata();
	pdo_delete('core_members_my',array('id'=>$post['myid'],'uniacid'=>$_SESSION['uniacid']));
	exi('删除成功！','','core/member.mymid/');
}